package com.sunlands.com.commands;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixThreadPoolKey;

/**
 * Created by yusong on 2018/4/28.
 */
public class CommandHelloWorld extends HystrixCommand<String> {

    private final String name;

    /**
    *cache the Setter
    */
    private static final Setter cachedSetter =
            Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                    .andCommandKey(HystrixCommandKey.Factory.asKey("HelloWorld")).
                    andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("HelloWorldPool"));

    public CommandHelloWorld(String name) {
        super(cachedSetter);
        this.name = name;
    }

    @Override
    protected String run() {
        // a real example would do work like a network call here
        return "Hello " + name + "!";
    }

    /**
    *出错默认回调
    */
    @Override
    protected String getFallback() {
        return "Hello Failure " + name + "!";
    }

}